/*
 * @Author: Tommy Chen weiyao_85@163.com
 * @Date: 2024-05-08 09:53:39
 * @LastEditors: Tommy Chen weiyao_85@163.com
 * @LastEditTime: 2024-05-08 14:35:12
 * @FilePath: /macc.admin.vue/src/project/authSys/store/modules/account/index.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { localRouter, getMenu, formatterMenu } from './localRouter';
import {
  setAsyncRoutes,
  updateAsyncRoutes,
} from '@project/authSys/router/async';
const state = {
  userRouter: [],
  userMenu: [],
  userMenuPath: {},
  scene: null,
};

const mutations = {
  SET_USER_ROUTER(state, payload) {
    state.userRouter = [...payload];
    setAsyncRoutes(payload);
  },
  SET_USER_MENU(state, payload) {
    state.userMenu = [...payload];
  },
  SET_USER_MENUPATH(state, payload) {
    state.userMenuPath[payload.key] = [...payload.path];
  },
  SET_USER_MENUSCENE(state, payload) {
    state.scene = payload;
  },
  UPDATE_USER_ROUTER(state, payload) {
    //更新路由
    state.userRouter = [...payload];
    updateAsyncRoutes(payload);
  },
};

const actions = {
  async getUserRouter({ commit, rootState }, params = {}) {
    const { theme } = rootState?.Theme;
    const { update } = params;
    // const layout = theme == 'NOC' ? 'BOTH' : 'TOP_MENU';
    const layout = 'TOP_MENU';
    //return new Promise((resolve) => {
    let requestRouter = [];

    requestRouter = localRouter(layout);
    //判断是否是内嵌框架
    if (window === window.top || window.top?.base?.mpm === 'intl_public') {
      //获取菜单数据
      const { moduleTree, scene } = await getMenu(theme);
      commit('SET_USER_MENU', moduleTree);
      commit('SET_USER_MENUSCENE', scene);
      //格式化菜单数据
      formatterMenu(moduleTree, requestRouter, commit);
    }
    if (update) {
      commit('UPDATE_USER_ROUTER', requestRouter);
    } else {
      commit('SET_USER_ROUTER', requestRouter);
    }
    //resolve();
    //});
  },
};

const getters = {};

export default {
  namespaced: true, //开启命名空间，更好的区分使用stage,防止不同stage模块中的方法重名
  state,
  getters,
  mutations,
  actions,
};
